package com.dao.impl;

import com.Bean.Consumer;
import com.dao.ConsumerDao;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ConsumerDaoImpl implements ConsumerDao {
    @Override
    public int insert(Consumer consumer) throws SQLException{
        String sql = "INSERT INTO goods(gid,gtid,gname,gprice) VALUES(?,?,?,?)";

        return  JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                consumer.getGid(),consumer.getType(),consumer.getName(),consumer.getPrice()
        });
    }

    @Override
    public List<Consumer> queryData(Consumer consumer) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "SELECT t.gtype type,g.gname name,g.gprice price FROM goodstype t JOIN goods g WHERE t.gtid=g.gtid";

        return JdbcUtil.queryData(sql, Consumer.class);
    }

    @Override
    public int update(Consumer consumer) throws SQLException {
        String sql = "UPDATE `goods` SET gtid=?,gname=?,gprice=? WHERE gid=?";

        return  JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                consumer.getType(),consumer.getName(),consumer.getPrice(),consumer.getGid()
        });
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        String sql = "DELETE FROM goods WHERE gid=?";

        return JdbcUtil.insertOrUpdateOrDelete(sql, new Object[]{id});
    }


    @Override
    public PageUtil<Consumer> queryDataBypage(int currentPgae, int pageSize, Consumer t) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "SELECT g.gid,t.gtype type,g.gname name,g.gprice price FROM goodstype t JOIN goods g WHERE t.gtid=g.gtid";
        ArrayList values = new ArrayList();

        return JdbcUtil.queryDataByPage(sql,Consumer.class, currentPgae,pageSize ,values.toArray());
    }
}
